Grails : Functional Testing
This page last changed on Feb 20, 2006 by mittie.
Functional TestingGrails supports functional testing of your web application. For this purpose, it uses the free open-source How to test your web applicationInstall and createFrom the root directory of your application call grails create-webtest
This results in the following actions
To scaffold a webtest for a given domain class use grails generate-webtest and enter the name of the domain class when prompted.
Running the tests.From the root directory of your application call grails run-webtest
Runs the tests and puts reports in the reports dir. When on Windows, the browser is automatically opened on the HTML report. It will present you a header with summarized and statistical data like below. A trailing section shows details about all operations effected during the test (excerpt). Note that WebTest stores all result pages it received from the server.
Defining your testsThe webtest folder layoutAfter creation and generation of a test for the mydomain domain class, you'll find the following files and folders below your application folder: myapp +-- webtest +-- conf (webtest.properties) +-- reports (readme.txt) +-- tests (TestSuite.groovy, MydomainTest.groovy)
Notable places for costomization are
Adapting the test logicMydomainTest.groovy will initially contain lines like class MydomainTest extends grails.util.WebTest { // Unlike unit tests, functional tests are often sequence dependent. // Specify that sequence here. void suite() { testMydomainListNewDelete() // add tests for more operations here } def testMydomainListNewDelete() { webtest('Mydomain basic operations: view list, create new entry, back to view, delete, view'){ invoke(url:'mydomain') verifyText(text:'Home') verifyListPage(0) // <- internal method call for extracting common steps clickLink(label:'New Mydomain') verifyText(text:'Create Mydomain') clickButton(label:'Create') // more ... }
There are multiple steps like invoke, clickButton, verifyXXX, etc. that make up the test specification. Find the full list of available steps, their parameters, a comprehensive description, and examples under WebTest Docs. The online documentation lists these steps in their ANT notation (i.e. XML). This exactly maps to the builder calls inside the webtest() methods in MyappTest.groovy. Behind the scenes, there is an AntBuilder that cares for the mapping, effectively producing a Groovy-API on top of Canoo WebTest. Adapt the lines in MydomainTest.groovy to match the expected behavior of your webapp. If you have any bootstrapped data, your tests can rely on this data being available when starting the tests. More TestsYou can have many tests like MydomainTest.groovy in the tests directory or its subdirectories. Every such test must extend grails.util.WebTest and provide a suite method that calls each available test method. In subdirectories, tests must have the according package statements. Tests will automatically be picked up by TestSuite.groovy. You can customize this logic to define your own suite (see its inlined comments on how to do that). Customization of the suite is sometimes needed to assert a special sequence of test execution or to restrict the test execution to a subset of availabable tests. groovy testing ![]() ![]() ![]() ![]() ![]() |
![]() |
Document generated by Confluence on Mar 29, 2006 08:46 |